$(function() {
	youtube_index.run();
});

var youtube_index = {
	run : function() {
		this._init_();
		this._start_();
		this._init_events();		
	},

	_init_ : function() {				
		this.$currTab = $("#today_div");		
		this.period = "today";		
		this.firstTab = 0;
		this.vdInstalled = vd.check();
		//key is this.period -> int
		this.queryPageCountMap = {};
		//key is this.period -> int
		this.queryPageMap = {};
		
		var path = location.pathname;
		if(path.indexOf("/world/featured/") >= 0){
			this.isI18n = true;
		}
	},

	_start_ : function() {
		var periods = ['today', 'this_week', 'this_month', 'all_time'];
		for ( var int = 0; int < periods.length; int++) {
			var period = periods[int];
			if(period == 'today'){
				this.queryPageMap[period] = 1;
				this.queryPageCountMap[period] = parseInt($("#today_page_count").val());
			}else{
				this.queryPageMap[period] = 0;	
				this.queryPageCountMap[period] = 0;
			}			
		}
		
		if(this.isI18n){
			$("#side_menu > ul > li").eq(0).addClass("curr");
		}
		
		var ua = navigator.userAgent;
		if (ua.indexOf('Mac') >= 0) {
			this.$alertDialog = $(".donwolad-alert-mac");
		} else {
			this.$alertDialog = $(".donwolad-alert");
		}
		
		this._handle_loc_args();
		
		//if(this.isI18n){
			this._adjust_layout();
		//}
	},
	
	_handle_loc_args: function(){
		var loc = location.href;
		var index = loc.indexOf("#");
		if(index > 0){
			var args = (loc.substring(index + 1)).getArgs();
			if(args['period']){
				this.period = args['period'];
			}
			
			if(args['genre']){
				this.genre = args['genre'];
			}
			
			var periods = ['today', 'this_week', 'this_month', 'all_time'];			
			//判断tab页面
			for ( var int = 0; int < periods.length; int++) {
				var period = periods[int];
				if(period == this.period){
					this.firstTab = int;
				}
			}		
			
			if(this.genre){
				$("#genere_select > option").each(function(){
					var $this = $(this);
					if(youtube_index.genre == $this.val()){
						$this.attr("selected", "selected");
						return false;
					}
				});
				
				if(this.firstTab == 0){
					//清空today页面
					$("#today_div").html($("#loading_div").html());
					//驱动搜索
					this._query_youtubes();
				}
			}
		}
	},

	_init_events : function() {
		
		//if(this.isI18n){
			$(window).resize(function() {
				youtube_index._adjust_layout();
			});
			
			$("#open_side_btn").click(function(){
				youtube_index._adjust_layout("open");
			});
			
			$("#close_side_btn").click(function(){
				youtube_index._adjust_layout();
			});
			
			$("#gotoTop").easeup();
		//}
		
		if(this.isI18n){
			$("#gotoTop").easeup();
		}
		
		$(".img").live({
			mouseenter:function(){
				$(this).children('div.tc_btn').fadeIn("slow");
			},
			mouseleave:function(){
				$(this).children('div.tc_btn').stop(true,false).fadeOut("fast");
			}
		});
		
		$("#genere_select").change(function(){			
			youtube_index.genre = $(this).val();			
			
			//重置page,pagecount
			for ( var key in youtube_index.queryPageMap) {
				youtube_index.queryPageMap[key] = 0;
				youtube_index.queryPageCountMap[key] = 0;
			}
			
			//清空页面
			$("#today_div, #this_week_div, #this_month_div, #all_time_div").html($("#loading_div").html());
			
			youtube_index._change_location();
			youtube_index._query_youtubes();
		});
		
		$("#colloection_cont").tabs({
			hoverClass: 'curr',
			onswitch: function($nav, $tab, $this){					
				youtube_index._handle_switch($nav, $tab);
				youtube_index._adjust_layout();
			},
			firstTab: this.firstTab
		});
		
		$.scroll(function(){
			youtube_index._handle_scroll();
		});
		
		//youtube_index._bind_tips_event();
	},
	
	_bind_tips_event : function(result) {
		var $div = null;
		if(result){
			$div = $("<div>" + result + "</div>");		
		}		
		
		//var $img = ($div != null) ? $(".img", $div) : $(".img");
		var $btn = ($div != null) ? $(".btn_down4", $div) : $(".btn_down4");
		
		/*
		$img.hover(
			  function () {
				  $(this).children("div.tc_btn").fadeIn("slow");
			  },
			  function () {
				  $(this).children("div.tc_btn").stop(true,false).fadeOut("fast");
			  }
		);
		*/
		
		$btn.click(function() {
			var itemUrl = $(this).attr("itemUrl");
			var title = $(this).attr("itemTitle");			
			if ( ! youtube_index.vdInstalled) {
				youtube_index.$alertDialog.dialog({
					submit : function() {
						vd.keep();
						_gaq.push([ '_trackEvent', 'Video', 'Download', itemUrl ]);
						vd.go(itemUrl, title);
						youtube_index.vdInstalled = true;
					},
					open: true
				});				
			} else {
				_gaq.push([ '_trackEvent', 'Video', 'Download', youtube_index.itemUrl ]);
				vd.go(youtube_index.itemUrl, youtube_index.title);
			}
			return false;
		});
	},
	
	_handle_switch: function($nav, $tab){
		this.$currTab = $tab;
		this.period = $nav.attr("period");
		var page = this.queryPageMap[this.period];
		if(page == 0){
			youtube_index._query_youtubes();			
		}
		youtube_index._change_location();
	},
	
	_change_location: function(){
		var args = "period=" + this.period;
		
		if(this.genre){
			args += "&genre=" + this.genre;
		}	
		
		var loc = location.href;
		var index = loc.indexOf("#");
		if(index > 0){
			loc = loc.substring(0, index);
		}
		location.href = loc + "#" + args;
	},
	
	_query_youtubes: function(page){
		var $w = $(window);
		if($w.data("querying")){
			return;
		}
		$w.data("querying", true);
		
		var period = this.period;
		var $loading = $("#" + period + "_div > div.loadingBox");
		$loading.show();
		
		page = page || 1;
		var url = null;
		if(location.pathname.indexOf("/world/") != -1){
			url = main.contextPath + "/world/featured/youtube/query?page=" + page;
		}else{
			url = main.contextPath + "/featured/youtube/query?page=" + page;
		}
		if(this.genre){
			url += "&genre=" + encodeURIComponent(this.genre);
		}
		if(period){
			url += "&period=" + period;
		}
		
		$.get(url, function(result){			
			$loading.before(result);
			//youtube_index._bind_tips_event(result);
			youtube_index.queryPageMap[period] = page;
			if(page == 1){
				//pageCount
				var count = $("#" + period + "_page_count").val();
				youtube_index.queryPageCountMap[period] = parseInt(count);
			}
		}).complete(function(){
			$w.data("querying", false);
			$w.data("scrolling", false);
			$loading.hide();
			//if(youtube_index.isI18n){
				youtube_index._adjust_layout();
			//}
		});
	},
	
	_handle_scroll: function(){
		var $w = $(window);
		if($w.data("scrolling")){
			return;
		}
		$w.data("scrolling", true);
		
		var page = this.queryPageMap[this.period];
		var pageCount = this.queryPageCountMap[this.period];
		if(pageCount < 2 || page == pageCount){
			return;
		}	
		this._query_youtubes(page + 1);				
	},
	
	_adjust_layout: function(action){
		/*var boxWidth = $("#colloection_cont").width()-10;
		var account = Math.floor(boxWidth/146);
		var paddingValue = 13 + (boxWidth - 146*account)/(account*2);
		//alert(boxWidth + "  " + paddingValue + " " + account);
		youtube_index.$currTab.css("width",boxWidth);
		youtube_index.$currTab.children("div.m_box").css("padding-right",paddingValue);
		youtube_index.$currTab.children("div.m_box").css("padding-left",paddingValue);*/
		
		var boxWidth = youtube_index.$currTab.width();
		if(action == 'open'){
			boxWidth = boxWidth - 180;
		}
		var account = Math.floor(boxWidth/165);
		var paddingValue = 20 + (boxWidth - 165*account + 20)/(account-1);
		var flag = 1;
		youtube_index.$currTab.children("div.m_box").each(function(){
			$(this).css("padding-right",0);
			if(flag < account){
				if(flag == 1){
					$(this).css("padding-left",0);
					flag++;
				}else{
					$(this).css("padding-left",paddingValue);
					flag++;
				}
			}else{
				$(this).css("padding-left",paddingValue);
				flag = 1;
			}
		});
	}
};